package com.shengdou.as.slopsapptest.Fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shengdou.as.slopsapptest.R;
import com.shengdou.as.slopsapptest.base.BaseFragment;
import com.shengdou.as.slopsapptest.bean.AlarmListBean;
import com.shengdou.as.slopsapptest.utils.Constants;
import com.shengdou.as.slopsapptest.utils.DateUtils;
import com.shengdou.as.slopsapptest.utils.HttpUtils;
import com.shengdou.as.slopsapptest.utils.LogUtil;
import com.shengdou.as.slopsapptest.utils.NetConstant;

import java.util.ArrayList;

/**
 * 报警记录
 */
public class NotesFragment extends BaseFragment {
    private ListView mListview;
    private ArrayList<AlarmListBean.DataBean> arrayList = new ArrayList<>();
    private MyAdapter adapter = new MyAdapter(arrayList);
    private LinearLayout gone;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constants.ALARMLIST:
                    String result = msg.obj.toString();
                    LogUtil.e("NotesFragmnetResult", result);

                    AlarmListBean managerInfo = new Gson().fromJson(result, AlarmListBean.class);
                    if (managerInfo.getData() != null) {
                        arrayList.clear();
                        arrayList.addAll(managerInfo.getData());
                        LogUtil.e("NotesFragmnetarrayList", arrayList.size() + "");
                    }

                    if (arrayList.size() == 0) {
                        adapter.notifyDataSetChanged();
                        mListview.setVisibility(View.GONE);
                        gone.setVisibility(View.VISIBLE);
                    } else {
                        mListview.setVisibility(View.VISIBLE);
                        gone.setVisibility(View.GONE);
                        adapter.notifyDataSetChanged();
                    }
                    break;
            }
        }
    };

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_notes, null);
    }

    @Override
    protected void initFindViewById(View view) {
        mListview = (ListView) view.findViewById(R.id.listview);
        gone = (LinearLayout) view.findViewById(R.id.txt_gone);
        mListview.setAdapter(adapter);
    }

    @Override
    public void initData() {
        LogUtil.e("initData=====NotesFragment=========", "initData");
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtil.e("onPause===========NotesFragment==================", "onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtil.e("onStop========NotesFragment============", "onStop");
    }

    @Override
    public void onResume() {
        super.onResume();
        HttpUtils.post(mHandler, NetConstant.ALARMLIST(), Constants.ALARMLIST, getActivity());
        LogUtil.e("onResume=====NotesFragment=========", "onResume");
    }

    private class MyAdapter extends BaseAdapter {
        private ArrayList<AlarmListBean.DataBean> mArray;

        public MyAdapter(ArrayList<AlarmListBean.DataBean> arrayList) {
            this.mArray = arrayList;
        }

        @Override
        public int getCount() {
            return mArray.size();
        }

        @Override
        public Object getItem(int position) {
            return mArray.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(mActivity, R.layout.item_notes_info, null);
                holder.mTxtTime = (TextView) convertView.findViewById(R.id.txt_time);
                holder.mTxtName = (TextView) convertView.findViewById(R.id.txt_name);
                holder.mTxtPool = (TextView) convertView.findViewById(R.id.txt_pool);
                holder.mTxtStatus = (TextView) convertView.findViewById(R.id.txt_status);
                holder.mTxtDistance = (TextView) convertView.findViewById(R.id.txt_distance);
                holder.mTxtType = (TextView) convertView.findViewById(R.id.txt_type);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.mTxtTime.setText(DateUtils.stringFormateData(mArray.get(position).getCreate_time()));
            holder.mTxtName.setText(mArray.get(position).getFactory_name());
            holder.mTxtPool.setText(mArray.get(position).getPools_name());
            double height = mArray.get(position).getHeight();
            holder.mTxtDistance.setText(height + "米");
            int alarm = mArray.get(position).getAlarm();
            if (alarm == 0) {
                holder.mTxtStatus.setText("正常");
                holder.mTxtStatus.setTextColor(getResources().getColor(R.color.green));
            } else if (alarm == 1) {
                holder.mTxtStatus.setText("橙色报警");
                holder.mTxtStatus.setTextColor(getResources().getColor(R.color.orange));
            } else if (alarm == 2) {
                holder.mTxtStatus.setText("红色报警");
                holder.mTxtStatus.setTextColor(getResources().getColor(R.color.red));
            }

//            holder.mTxtDistance.setText(mArray.get(position).getHeight() + "米");
            if (mArray.get(position).getChange_status() == -1) {
                holder.mTxtType.setText("警报降级通知");
            } else if (mArray.get(position).getChange_status() == 1) {

                holder.mTxtType.setText("警报升级通知");
            }
            return convertView;
        }
    }

    class ViewHolder {
        TextView mTxtTime;
        TextView mTxtName;
        TextView mTxtPool;
        TextView mTxtStatus;
        TextView mTxtDistance;
        TextView mTxtType;
    }
}
